﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YY.UI.BaseDataSvc;

namespace YY.UI
{
    public partial class EditCaseFrm : BaseFrm
    {
        /// <summary>
        /// 箱号所属工作单
        /// </summary>
        public TB_Order Order { get; set; }

        /// <summary>
        /// 编辑模式时传入id
        /// </summary>
        public int ID { get; set; }

        /// <summary>
        /// 用户临时变量
        /// </summary>
        TB_Case _Case = null;

        /// <summary>
        /// 是否编辑模式
        /// </summary>
        public bool EditMode { get; set; }

        public EditCaseFrm()
        {
            InitializeComponent();
            this.BaseDataProxy = new YY.UI.BaseDataSvc.BaseDataClient();
        }

        private void EditCaseFrm_Load(object sender, EventArgs e)
        {
            this.BaseDataProxy.Open();
            if (this.EditMode)
                bindData(this.ID);
        }

        private void bindData(int id)
        {
            this._Case = this.BaseDataProxy.GetCase(id);
            if (this._Case != null)
            {
                this.txtBoxCarNo.Text = this._Case.carNo;
                this.txtBoxCheck.Text = this._Case.check;
                this.txtBoxCode.Text = this._Case.code;
                this.txtBoxDriver.Text = this._Case.driver;
                this.txtBoxMemo.Text = this._Case.memo;
                this.txtBoxModel.Text = this._Case.model;
                //this.txtBoxMotorcade.Text = this._Case.motorcade;
                this.txtBoxTel.Text = this._Case.tel;
                this.txtBoxTitle.Text = this._Case.title;
                this.txtBoxTrailer.Text = this._Case.trailer;
                //this.Order = this._Case.order;
            }
        }

        private void tsBtnAdd_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.groupBox1.Controls.Count; i++)
            {
                if (this.groupBox1.Controls[i].Name.StartsWith("txtBox"))
                    this.groupBox1.Controls[i].Text = null;
            }

            this.EditMode = false;
            this.ID = 0;
        }

        private void tsBtnSave_Click(object sender, EventArgs e)
        {
            if (Common.BIZ_OrderIsSubmit(this.Order.id))
            {
                MessageBox.Show("已提交财务审核单据不能修改！");
                return;
            }

            if (string.IsNullOrEmpty(this.txtBoxCode.Text))
            {
                MessageBox.Show("请输入箱号！");
                this.txtBoxCode.Focus();
                return;
            }

            if (string.IsNullOrEmpty(this.txtBoxModel.Text))
            {
                MessageBox.Show("请输入箱型！");
                this.txtBoxModel.Focus();
                return;
            }

            TB_Case c = new TB_Case();
            c.carNo = this.txtBoxCarNo.Text;
            c.check = this.txtBoxCheck.Text;
            c.code = this.txtBoxCode.Text;
            c.driver = this.txtBoxDriver.Text;
            c.memo = this.txtBoxMemo.Text;
            c.model = this.txtBoxModel.Text;
            //c.motorcade = this.txtBoxMotorcade.Text;
            //c.TB_Cargo = null;
            c.tel = this.txtBoxTel.Text;
            c.title = this.txtBoxTitle.Text;
            c.trailer = this.txtBoxTrailer.Text;

            Common.OperateType opType = Common.OperateType.Add;
            bool ok = false;
            if (this.EditMode)
            {
                c.id = this._Case.id;
                c.order = this._Case.order;
                opType = Common.OperateType.Update;
                ok = this.BaseDataProxy.UpdateCase(c);
            }
            else
            {
                c.order = this.Order.id;
                this.ID = this.BaseDataProxy.AddCase(c);
                ok = this.ID > 0;
                c.id = this.ID;
            }

            if (ok)
            {
                this._Case = c;
                this.EditMode = true;
                CommonEventArg<TB_Case> arg = new CommonEventArg<TB_Case>(c, opType);
                YY.Common.EventCommon.OnCaseUdDGVEvnt(sender, arg);
            }
            else
                MessageBox.Show("保存异常！");
        }

        private void tsBtnDelete_Click(object sender, EventArgs e)
        {
            if (this.Order.isSubmit)
            {
                MessageBox.Show("已提交财务审核单据不能修改！");
                return;
            }

            if (this.EditMode && this._Case!=null)
            {
                if (MessageBox.Show("确认删除该项吗？", "删除提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    if (this.BaseDataProxy.DeleteCase(this._Case.id))
                    {
                        CommonEventArg<TB_Case> arg = new CommonEventArg<TB_Case>(this._Case, Common.OperateType.Delete);
                        YY.Common.EventCommon.OnCaseUdDGVEvnt(sender, arg);
                        MessageBox.Show("箱号资料已删除！");
                        this.Close();
                    }
                    else
                        MessageBox.Show("删除异常！");
                }
            }

        }
    }
}
